package com.iwat.hellospringboot.service;

/**
 * @author 王伟强
 * @date 2024/5/6 15:18
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class NotificationService {

    private final SimpMessagingTemplate template;

    @Autowired
    public NotificationService(SimpMessagingTemplate template) {
        this.template = template;
    }

    public void notify(String destination, String message) {
        // 在这里，"/topic/greetings"是消息的目的地，message是要发送的消息
        this.template.convertAndSend(destination, message);
        System.out.println("消息发送成功");
    }

    public void handle(String message) throws InterruptedException{
        //模拟业务处理
        TimeUnit.SECONDS.sleep(2);
        // 在这里，"/topic/handle"是消息的目的地，message是要发送的消息
        //notify("/topic/handle", "Hello ".concat(message));
    }
}